package com.arsn.myf.system.controller;

import com.arsn.myf.common.util.result.Func;
import com.arsn.myf.common.util.result.R;
import com.arsn.myf.system.entity.Role;
import com.arsn.myf.system.service.RoleService;
import com.arsn.myf.system.vo.RoleMenuVO;
import com.arsn.myf.system.vo.RoleVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.wildfly.common.annotation.NotNull;
import java.util.Arrays;

/**
 * 角色
 * @author zhongrj
 * @since 2022-07-10
 */
@RestController
@RequestMapping("/role")
@AllArgsConstructor
@Api(value = "角色", tags = "角色")
public class RoleController {

    private final RoleService roleService;

    /**
     * 查询角色的分页数据
     * @param page
     * @param role
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询角色的分页数据")
    public R list(Page<Role> page, Role role){
        return R.data(roleService.page(page,new QueryWrapper<>(role)));
    }


    /**
     * 自定义查询角色的分页数据
     * @param role
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "自定义查询角色的分页数据")
    public R page(RoleVO role){
        return R.data(roleService.selectPageRoleList(role));
    }


    /**
     * 角色详情信息
     * @param role
     * @return
     */
    @GetMapping("/detail")
    @ApiOperation(value = "角色详情信息")
    public R detail(Role role){
        return R.data(roleService.getOne(new QueryWrapper<>(role)));
    }


    /**
     * 角色新增
     * @param role
     * @return
     */
    @PostMapping("/save")
    @ApiOperation(value = "角色新增")
    public R save(@RequestBody Role role){
        return R.status(roleService.save(role));
    }

    /**
     * 角色修改
     * @param role
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "角色修改")
    public R update(@RequestBody Role role){
        return R.status(roleService.updateById(role));
    }


    /**
     * 删除,批量删除
     * @param ids
     * @return
     */
    @PostMapping("/remove")
    @ApiOperation(value = "删除,批量删除")
    public R remove(@NotNull String ids){
        return R.status(roleService.removeByIds(Func.stringToLong(Arrays.asList(ids.split(",")))));
    }

    /**
     * 查询角色列表
     * @param role
     * @return
     */
    @GetMapping("getRoleList")
    @ApiOperation(value = "查询角色列表")
    public R getRoleList(RoleVO role){
        return R.data(roleService.getRoleList(role));
    }


    /**
     * 获取角色树
     * @param role 角色查询参数对象
     * @return
     */
    @GetMapping("getRoleTree")
    @ApiOperation(value = "获取角色树")
    public R getRoleTree(RoleVO role){
        return R.data(roleService.getRoleTree(role));
    }

    /**
     * 角色菜单配置
     * @param roles (roleId 角色id,menuIds 菜单id)
     * @return
     */
    @PostMapping("/updateMenuRole")
    @ApiOperation(value = "角色菜单配置")
    public R updateMenuRole(@RequestBody RoleMenuVO roles){
        return R.status(roleService.updateMenuRole(roles));
    }
}
